package cc.chinagps.gateway.unit.eg.util;

import cc.chinagps.gateway.buff.GBossDataBuff;
import cc.chinagps.gateway.buff.GBossDataBuff.EGEngineDataStreamInfo;
import cc.chinagps.gateway.buff.GBossDataBuff.EGObdInfo;
import cc.chinagps.gateway.buff.GBossDataBuff.ExhaustGasInfo;
import cc.chinagps.gateway.unit.eg.pkg.EGPackage;
import cc.chinagps.gateway.unit.eg.upload.bean.*;
import com.google.protobuf.ByteString;

import java.util.Date;
import java.util.List;

public class EGProtobufUtil {
	/*public static cc.chinagps.gateway.buff.GBossDataBuff.GpsInfo transformGPSInfo(String callLetter, EGPackage pkg,
																				  EGAlarm egAlarm) {
		cc.chinagps.gateway.buff.GBossDataBuff.GpsInfo.Builder builder = GBossDataBuff.GpsInfo.newBuilder();
		builder.setCallLetter(callLetter);

		cc.chinagps.gateway.buff.GBossDataBuff.GpsBaseInfo.Builder baseBuilder = GBossDataBuff.GpsBaseInfo.newBuilder();
		baseBuilder.setGpsTime(egAlarm.getGpsTime());
		baseBuilder.setLoc(egAlarm.getLocationState() == 0);
		baseBuilder.setLat(egAlarm.getLat());
		baseBuilder.setLng(egAlarm.getLng());
		baseBuilder.setCourse(0);
		baseBuilder.setSpeed(0);
		baseBuilder.setReceiveTime(new Date().getTime());
		if (egAlarm.getAlarmTypeList().size() > 0) {
			baseBuilder.addAllStatus(egAlarm.getAlarmTypeList());
		}

		GBossDataBuff.ExhaustGasInfo.Builder exhaustGasInfoBuilder = ExhaustGasInfo.newBuilder();
		GBossDataBuff.EGEngineDataStreamInfo.Builder dataStreamBuilder = EGEngineDataStreamInfo.newBuilder();
		dataStreamBuilder.setLat(egAlarm.getLat());
		dataStreamBuilder.setLng(egAlarm.getLng());
		dataStreamBuilder.setLocationState(egAlarm.getLocationState());
		exhaustGasInfoBuilder.setEgEngineInfo(dataStreamBuilder.build());
		baseBuilder.setExhaustGasInfo(exhaustGasInfoBuilder.build());

		builder.setBaseInfo(baseBuilder.build());
		builder.setContent(ByteString.copyFrom(pkg.getSource()));

		return builder.build();
	}*/

    public static cc.chinagps.gateway.buff.GBossDataBuff.GpsInfo transformGPSInfo(String callLetter, EGPackage pkg,
                                                                                  EGRealTimeInfo egRealTimeInfo) {
        cc.chinagps.gateway.buff.GBossDataBuff.GpsInfo.Builder builder = GBossDataBuff.GpsInfo.newBuilder();
        builder.setCallLetter(callLetter);

        cc.chinagps.gateway.buff.GBossDataBuff.GpsBaseInfo baseBuilder = transGPSBaseInfo(callLetter, egRealTimeInfo);
        builder.setBaseInfo(baseBuilder);
        builder.setContent(ByteString.copyFrom(pkg.getSource()));

        return builder.build();
    }

    private static cc.chinagps.gateway.buff.GBossDataBuff.GpsBaseInfo transGPSBaseInfo(String callLetter,
                                                                                       EGRealTimeInfo egRealTimeInfo) {
        cc.chinagps.gateway.buff.GBossDataBuff.GpsBaseInfo.Builder baseBuilder = GBossDataBuff.GpsBaseInfo.newBuilder();
        baseBuilder.setGpsTime(egRealTimeInfo.getCollectTime());
        baseBuilder.setLoc(egRealTimeInfo.getDataStreamInfo().getLocationState() == 0);
        baseBuilder.setLat(egRealTimeInfo.getDataStreamInfo().getLat());
        baseBuilder.setLng(egRealTimeInfo.getDataStreamInfo().getLng());
        baseBuilder.setSpeed(egRealTimeInfo.getDataStreamInfo().getSpeed() / 100);
        baseBuilder.setCourse(0);
        baseBuilder.setReceiveTime(new Date().getTime());
        if (egRealTimeInfo.getStatusList().size() > 0) {
            baseBuilder.addAllStatus(egRealTimeInfo.getStatusList());
        }

        if (egRealTimeInfo.isHistory()) {
            baseBuilder.addStatus(400);
        }
        GBossDataBuff.ExhaustGasInfo.Builder exhaustGasInfoBuilder = ExhaustGasInfo.newBuilder();

        cc.chinagps.gateway.unit.eg.upload.bean.EGObdInfo obdInfo = egRealTimeInfo.getEgObdInfo();
        cc.chinagps.gateway.unit.eg.upload.bean.EGEngineDataStreamInfo dataStreamInfo = egRealTimeInfo
                .getDataStreamInfo();

        if (obdInfo != null) {
            GBossDataBuff.EGObdInfo.Builder obdInfoBulider = EGObdInfo.newBuilder();
            obdInfoBulider.setCvn(obdInfo.getCvn());
            obdInfoBulider.setDiagReadyState(obdInfo.getDiagReadyState());
            obdInfoBulider.setDiagSupportState(obdInfo.getDiagSupportState());
            obdInfoBulider.addAllFaultCodeList(obdInfo.getFaultCodeList());
            obdInfoBulider.setFaultCodeNum(obdInfo.getFaultCodeNum());
            obdInfoBulider.setIupr(obdInfo.getIupr());
            obdInfoBulider.setMilState(obdInfo.getMilState());
            obdInfoBulider.setObdDiagProtocol(obdInfo.getObdDiagProtocol());
            obdInfoBulider.setSoftwareCaliCode(obdInfo.getSoftwareCaliCode());
            obdInfoBulider.setVin(obdInfo.getVin());
            exhaustGasInfoBuilder.setEgobdInfo(obdInfoBulider.build());
        }

        GBossDataBuff.EGEngineDataStreamInfo.Builder dataStreamBuilder = EGEngineDataStreamInfo.newBuilder();
        dataStreamBuilder.setAccePedel(dataStreamInfo.getAccePedel());
        dataStreamBuilder.setAirInput(dataStreamInfo.getAirInput());
        dataStreamBuilder.setAtmosPressure(dataStreamInfo.getAtmosPressure());
        dataStreamBuilder.setDpfDiffPress(dataStreamInfo.getDpfDiffPress());
        dataStreamBuilder.setDpfExhaustTemp(dataStreamInfo.getDpfExhaustTemp());
        dataStreamBuilder.setEngineCoolantTemp(dataStreamInfo.getEngineCoolantTemp());
        dataStreamBuilder.setEngineOutputTorque(dataStreamInfo.getEngineOutputTorque());
        dataStreamBuilder.setEngineTorqueMode(dataStreamInfo.getEngineTorqueMode());
        dataStreamBuilder.setFrictionTorque(dataStreamInfo.getFrictionTorque());
        dataStreamBuilder.setFuelFlow(dataStreamInfo.getFuelFlow());
        dataStreamBuilder.setFuelLevel(dataStreamInfo.getFuelLevel());
        dataStreamBuilder.setLat(dataStreamInfo.getLat());
        dataStreamBuilder.setLng(dataStreamInfo.getLng());
        dataStreamBuilder.setLocationState(dataStreamInfo.getLocationState());
        dataStreamBuilder.setRotationSpeed(dataStreamInfo.getRotationSpeed());
        dataStreamBuilder.setScrDownNoxOutput(dataStreamInfo.getScrDownNoxOutput());
        dataStreamBuilder.setScrInTemp(dataStreamInfo.getScrInTemp());
        dataStreamBuilder.setScrOutTemp(dataStreamInfo.getScrOutTemp());
        dataStreamBuilder.setScrUpNoxOutput(dataStreamInfo.getScrUpNoxOutput());
        dataStreamBuilder.setSpeed(dataStreamInfo.getSpeed());
        dataStreamBuilder.setTotalDistance(dataStreamInfo.getTotalDistance());
        dataStreamBuilder.setTotalOil(dataStreamInfo.getTotalOil());
        dataStreamBuilder.setTotalUreaUsed(dataStreamInfo.getTotalUreaUsed());
        dataStreamBuilder.setUreaBoxTemp(dataStreamInfo.getUreaBoxTemp());
        dataStreamBuilder.setUreaLevel(dataStreamInfo.getUreaLevel());
        dataStreamBuilder.setUreaSprayVolume(dataStreamInfo.getUreaSprayVolume());
        if (dataStreamInfo.getInstantFuel() != null) {
            dataStreamBuilder.setInstantFuel(dataStreamInfo.getInstantFuel());
        }
        dataStreamBuilder.setCourse(dataStreamInfo.getCourse());
        EGSmokeDetectInfo detectInfo = egRealTimeInfo.getEgSmokeDetectInfo();
        if (detectInfo != null) {
            cc.chinagps.gateway.buff.GBossDataBuff.EGSmokeDetectInfo.Builder detectInfoBuilder = cc.chinagps.gateway.buff.GBossDataBuff.EGSmokeDetectInfo
                    .newBuilder();
            detectInfoBuilder.setLac(detectInfo.getLac());
            detectInfoBuilder.setOpacity(detectInfo.getOpacity());
            detectInfoBuilder.setParticleConcentration(detectInfo.getParticleConcentration());
            detectInfoBuilder.setDriveCycles(detectInfo.getDriveCycles());
            exhaustGasInfoBuilder.setSmokeDetectInfo(detectInfoBuilder.build());
        }

        EGSuZhouInfo egSuZhouInfo = egRealTimeInfo.getEgSuZhouInfo();
        if (egSuZhouInfo != null) {
            GBossDataBuff.EGSuZhouInfo.Builder suzhouBuilder = GBossDataBuff.EGSuZhouInfo.newBuilder();
            suzhouBuilder.setIdlingTotalOil(egSuZhouInfo.getIdlingTotalOil());
            suzhouBuilder.setTotalEngineRunningTime(egSuZhouInfo.getTotalEngineRunningTime());
            suzhouBuilder.addAllStatus(egSuZhouInfo.getStatus());
            suzhouBuilder.setTotalIdlingTime(egSuZhouInfo.getTotalIdlingTime());
            suzhouBuilder.setReferTotalDistance(egSuZhouInfo.getReferTotalDistance());
            suzhouBuilder.setReferTotalOil(egSuZhouInfo.getReferTotalOil());
            exhaustGasInfoBuilder.setSuZhouInfo(suzhouBuilder.build());
        }

        exhaustGasInfoBuilder.setEgEngineInfo(dataStreamBuilder.build());
        exhaustGasInfoBuilder.setSoftwareVersion(egRealTimeInfo.getSoftwareVersion());

        baseBuilder.setExhaustGasInfo(exhaustGasInfoBuilder.build());
        return baseBuilder.build();
    }

    public static GBossDataBuff.EGAlarm transformEGAlarmInfo(String callLetter, EGPackage pkg, EGAlarmInfo egAlarmInfo) {
        GBossDataBuff.EGAlarm.Builder builder = GBossDataBuff.EGAlarm.newBuilder();
        builder.setDeviceId(callLetter);
        builder.setAlarmTime(egAlarmInfo.getAlarmTime());
        builder.setLocationState(egAlarmInfo.getLocationState());
        builder.setLat(egAlarmInfo.getLat());
        builder.setLng(egAlarmInfo.getLng());
        builder.setCourse(0);
        builder.setSpeed(0);
        builder.setReceiveTime(new Date().getTime());
        builder.addAllStatus(egAlarmInfo.getAlarmTypeList());
        builder.setContent(ByteString.copyFrom(pkg.getSource()));
        return builder.build();
    }

    public static GBossDataBuff.EGJMCInfo transformEGJMCInfo(String callLetter, EGPackage pkg, JMCDriving jmcDriving, JMCFault jmcFault) {
        GBossDataBuff.EGJMCInfo.Builder builder = GBossDataBuff.EGJMCInfo.newBuilder();
        builder.setDeviceId(callLetter);

        if (jmcDriving != null) {
            GBossDataBuff.JMCDriving.Builder builder1 = GBossDataBuff.JMCDriving.newBuilder();
            builder1.setEventTime(jmcDriving.getEventTime());
            builder1.setEventType(jmcDriving.getEventType());
            builder1.setLocationState(jmcDriving.getLocationState());
            builder1.setLat(jmcDriving.getLat());
            builder1.setLng(jmcDriving.getLng());
            builder1.setSpeed(jmcDriving.getSpeed());

            builder.setDriving(builder1.build());
        }

        if (jmcFault != null) {
            GBossDataBuff.JMCFault.Builder builder1 = GBossDataBuff.JMCFault.newBuilder();
            builder1.setFaultTime(jmcFault.getFaultTime());
            builder1.setDtcType(jmcFault.getDtcType());
            List<JMCEcuInfo> jmcEcuInfos = jmcFault.getEcuInfoList();
            if (jmcEcuInfos != null && jmcEcuInfos.size() != 0) {
                for (JMCEcuInfo jmcEcuInfo : jmcEcuInfos) {
                    GBossDataBuff.JMCECUInfo.Builder builder2 = GBossDataBuff.JMCECUInfo.newBuilder();
                    builder2.setEcuCode(jmcEcuInfo.getEcuCode());
                    List<JMCDTCInfo> jmcdtcInfos = jmcEcuInfo.getJmcDTCInfos();
                    if (jmcdtcInfos != null && jmcdtcInfos.size() != 0) {
                        for (JMCDTCInfo jmcdtcInfo : jmcdtcInfos) {
                            GBossDataBuff.JMCDTCInfo.Builder builder3 = GBossDataBuff.JMCDTCInfo.newBuilder();
                            builder3.setDtcCode(jmcdtcInfo.getDtcCode());
                            builder3.setFailureType(jmcdtcInfo.getFailureType());
                            builder3.setStatusCover(jmcdtcInfo.getStatusCover());
                            builder2.addDtcInfos(builder3.build());
                        }
                    }
                    builder1.addEcuInfos(builder2.build());
                }
            }
            builder.setFault(builder1.build());
        }

        builder.setReceiveTime(new Date().getTime());
        builder.setContent(ByteString.copyFrom(pkg.getSource()));
        return builder.build();
    }
}